package com.suse.warmstill.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.suse.warmstill.entity.TbRoleMenu;
import com.suse.warmstill.entity.TbRoleResource;
import com.suse.warmstill.mapper.TbRoleMenuMapper;
import com.suse.warmstill.mapper.TbRoleResourceMapper;
import com.suse.warmstill.service.RoleResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 黄靖
 * @author 2022-10-20 07:32
 * description:
 **/
@Service
public class RoleResourceServiceImpl implements RoleResourceService {

    @Autowired
    private TbRoleResourceMapper roleResourceMapper;


    @Override
    public List<Long> listResourceByRoleId(long roleId) {
        final List<Long> resourceList = roleResourceMapper.selectList(new LambdaQueryWrapper<TbRoleResource>().eq(TbRoleResource::getRoleId, roleId)).
                stream().map(TbRoleResource::getResourceId).collect(Collectors.toList());
        return resourceList;
    }

    @Override
    public void insertRoleResource(Long roleId, List<Long> resourceList) {
        resourceList.stream().map((resourceId) -> {
            final TbRoleResource tbRoleResource = new TbRoleResource();
            tbRoleResource.setRoleId(roleId);
            tbRoleResource.setResourceId(resourceId);
            roleResourceMapper.insert(tbRoleResource);
            return null;
        });
    }

    @Override
    public void deleteByRoleId(Long roleId) {
        final LambdaQueryWrapper<TbRoleResource> tbRoleResourceLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tbRoleResourceLambdaQueryWrapper.eq(TbRoleResource::getRoleId, roleId);
        roleResourceMapper.delete(tbRoleResourceLambdaQueryWrapper);
    }
}
